﻿# region
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Runtime.InteropServices;//捕捉错误
# endregion

# region Using ArcGIS
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.DataSourcesFile;
using ESRI.ArcGIS.esriSystem;//不用从引用加入
using ESRI.ArcGIS.Carto;//管理图层
using ESRI.ArcGIS.DataSourcesGDB;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Display;
# endregion

using popGis.LJP;

namespace popGis
{
    public partial class MainForm : Form
    {

        # region Members

        private IFeatureLayer pFeatureLayer = new FeatureLayerClass();    //要素图层
        private IWorkspace pWorkspace;                                 //工作空间
        public string mdbPath;                                    //个人数据库存放路径

        # endregion

        public MainForm()
        {
            InitializeComponent();
            mdbPath = @"E:\09343081\GIS_project\popGis\PopDB.mdb";
            OpenFeatureLayerFromFeatureClassName("gz", out pFeatureLayer);
        }

        # region Basic Functions

        private IEnvelope m_pEnvelope = null;

        private void mainMapControl_OnAfterDraw(object sender, ESRI.ArcGIS.Controls.IMapControlEvents2_OnAfterDrawEvent e)
        {
            // 绘制红色鹰眼框
            object pSym = null;
            SimpleLineSymbol pOutlineSym;
            IRgbColor pColor;
            pColor = new RgbColorClass();
            pColor.Red = 255; pColor.Green = 0; pColor.Blue = 0;

            pSym = new SimpleFillSymbolClass();
            ISimpleFillSymbol pSym1 = pSym as ISimpleFillSymbol;
            pSym1.Style = esriSimpleFillStyle.esriSFSHollow;

            pOutlineSym = new SimpleLineSymbolClass();
            pOutlineSym.Width = 1;
            pOutlineSym.Style = esriSimpleLineStyle.esriSLSSolid;
            pOutlineSym.Color = pColor;
            pSym1.Outline = pOutlineSym;

            if (m_pEnvelope == null) return;
            overviewMapControl.DrawShape(m_pEnvelope as IGeometry, ref pSym);
        }

        private void mainMapControl_OnExtentUpdated(object sender, IMapControlEvents2_OnExtentUpdatedEvent e)
        {
            try
            {
                ILayer pLayer = mainMapControl.get_Layer(0) as ILayer;
                overviewMapControl.AddLayer(pLayer);
            }
            catch (COMException ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }
            overviewMapControl.ActiveView.Refresh();
            m_pEnvelope = e.newEnvelope as IEnvelope;
        }

        private void overviewMapControl_OnMouseDown(object sender, IMapControlEvents2_OnMouseDownEvent e)
        {
            ESRI.ArcGIS.Geometry.Point pPoint = new ESRI.ArcGIS.Geometry.PointClass();
            pPoint.PutCoords(e.mapX, e.mapY);
            mainMapControl.CenterAt(pPoint);
        }

        private void mainMapControl_OnMouseMove(object sender, IMapControlEvents2_OnMouseMoveEvent e)
        {
            coordinateLabel.Text = "坐标: X = " + e.mapX.ToString("F6") + "  Y = " + e.mapY.ToString("F6");
        }

        # endregion

        private void MainForm_Load(object sender, EventArgs e)
        {
            axTOCControl1.SetBuddyControl(mainMapControl);// 设置图层控制器与主图层关联(设计界面TOC属性也同时要改)
            axTOCControl1.EnableLayerDragDrop = true;

            mainMapControl.AddLayer((ILayer)pFeatureLayer);// Add default layer to mainMapControl

            pWorkspace = null;
        }

        private void tbnDrawDensityPoint_Click(object sender, EventArgs e)
        {
            ILayer pLayer = mainMapControl.get_Layer(0) as ILayer;
            ArrayList itemList = new ArrayList();

            //PopDistributionGeometry myGeometry = new PopDistributionGeometry(pLayer, "tot_pop", 2000);
            //if(myGeometry.getShapeCenterList())
            //    itemList = myGeometry.m_ShapeCenter;
            //IElementCollection pEleColl = myGeometry.getElementCollection();

            ////计算分布中心
            //IPoint popCenter = DistributionCenter.CalculateDistributionCenter(itemList);
            //IElement pEle = VisualDisplay.IGeometry2IElement(popCenter);

            ////绘制
            //VisualDisplay.AddTempElement(mainMapControl, pEle, pEleColl);            
        }
        //查看属性表
        private void tbnAttributeTable_Click(object sender, EventArgs e)
        {
            ILayer pLayer = mainMapControl.get_Layer(0) as ILayer;
            AttributeForm frm = new AttributeForm(pLayer); frm.Show();
        }

        private void UniqueRender_Click(object sender, EventArgs e)
        {
            JoinTable.JoinData(OpenAccessWorkspace(mdbPath), pFeatureLayer, "基本信息表", "NAME", "地名");

            Render.UniqueValueRender((ILayer)pFeatureLayer, "基本信息表.总人口2006");
            //Render.ClassBreakRender((ILayer)pFeatureLayer, "基本信息表.总人口2006", 3);
            //Render.DotDensityRender((ILayer)pFeatureLayer, "基本信息表.总人口2006");

            mainMapControl.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, null);
            axTOCControl1.Update();

            //JoinTable.setFeatureLayerRelationshipNull(pFeatureLayer);
        }

        private void tbnDataTableRender_Click(object sender, EventArgs e)
        {
            AccessDataConnectingClass myDataset = new AccessDataConnectingClass();
            myDataset.CreateDTableOfBasicInfo();
            DataTable table = myDataset.DTableOfBasicInfo;
            DataGridForm frm = new DataGridForm(table); frm.Show();

            string itableName = "temptable";
            ITable pTable = DataConvertion.DataTable2ITable(OpenAccessWorkspace(mdbPath), table, itableName);

            JoinTable.JoinData(OpenAccessWorkspace(mdbPath), pFeatureLayer, itableName, "NAME", "地名");
            Render.UniqueValueRender((ILayer)pFeatureLayer, itableName+".总人口2006");
            mainMapControl.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, null);
            axTOCControl1.Update();
        }
        //取消关联
        private void tbnCleanRelship_Click(object sender, EventArgs e)
        {
            JoinTable.setFeatureLayerRelationshipNull(pFeatureLayer);
        }


    }
}
